package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.TbOrderService;
import com.dongdongshop.util.IdWorker;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("order")
public class TbOrderController {
    @DubboReference(check = false)
    private TbOrderService orderService;


    @RequestMapping("Shping")
    @ResponseBody
    private Result Shping(String itmeId,Long userId){
        IdWorker idWorker = new IdWorker();
        String id = String.valueOf(idWorker.nextId());
        try {
            Subject subject = SecurityUtils.getSubject();//获取用户
            TbUser user = (TbUser) subject.getPrincipal();//获取用户
            String[] itemIds = itmeId.split(",");
            double money = orderService.addOrder(itemIds,userId,user, Long.valueOf(id));
            Map map = new HashMap();
            map.put("id",id);
            map.put("money",money);
            map.put("userId",user.getId());
            return Result.OK().setData(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR();
        }
    }
    @RequestMapping("toPay")
    public String  toPay(){
        return "pay";
    }
}
